﻿// @brief
// @file IRegisterGroup.cs
// @date 2024年08月07日
// @author doublecat
// 
// Copyright (C) &lt;2024&gt;  &lt;doublecat&gt;
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

namespace doublecat.mcu.svd;

public interface IRegisterGroup {
    public string? Size { get; set; }

    public string? Access { get; set; }

    public string? Protection { get; set; }

    public string? ResetValue { get; set; }

    public string? ResetMask { get; set; }
}

/// <summary>
/// Register group 帮助类
/// </summary>
public static class RegisterGroupHelper {
    /// <summary>
    /// 解析 ProtectionEnum
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static ProtectionEnum? ToProtectionEnum(this string? str) {
        return str switch {
            "S" => ProtectionEnum.S,
            "N" => ProtectionEnum.N,
            "P" => ProtectionEnum.P,
            _   => null
        };
    }

    public static string? ToString(this ProtectionEnum? p) {
        return p == null ? null : Enum.GetName(p.Value);
    }

    /// <summary>
    /// 解析 AccessEnum
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static AccessEnum? ToAccessEnum(this string? str) {
        return str switch {
            "readOnly"      => AccessEnum.ReadOnly,
            "writeOnly"     => AccessEnum.WriteOnly,
            "readWrite"     => AccessEnum.ReadWrite,
            "writeOnce"     => AccessEnum.WriteOnce,
            "readWriteOnce" => AccessEnum.ReadWriteOnce,
            _               => null
        };
    }

    public static string? ToString(this AccessEnum? p) {
        return p == null ? null : Enum.GetName(p.Value);
    }
}

public enum ProtectionEnum {
    S,
    N,
    P
}

public enum AccessEnum {
    ReadOnly,
    WriteOnly,
    ReadWrite,
    WriteOnce,
    ReadWriteOnce
}
